package com.opengate.demo.api.base.mq;

import com.alibaba.fastjson.JSON;
import com.opengate.base.stream.RepInput;
import com.opengate.demo.contact.message.DemoMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;

/**
 * @author 曳戈泰尔
 * @version 1.0
 * @description 接收mq
 * @date 2023/4/27 8:59 PM
 */
@Slf4j
@Component
public class DemoConsumerMq {

    /**
     * Sink.INPUT的消息通道是广播模式，意味着它能够将消息传递给所有订阅该通道的消费者。
     *
     * <p>这种模式适用于需要多个消费者同时接收相同消息的场景。
     */
    @StreamListener(value = Sink.INPUT, condition = "headers['type'] == 'DemoMessage'")
    public void consumerDemoMessage(DemoMessage message) {
        log.info("consumerDemoMessage={}", JSON.toJSONString(message));
    }

    /** 单播模式 */
    @StreamListener(value = RepInput.INPUT, condition = "headers['type'] == 'DemoMessage'")
    public void consumerRepInputDemoMessage(DemoMessage message) {
        log.info("consumerRepInputDemoMessage={}", JSON.toJSONString(message));
    }
}
